package Send2ME::Delete::Rule;

use Send2ME::Dao::Auth;
use Send2ME::Dao::Rule;

use Dancer ':syntax';

prefix '/delete/rule';

get '/:id' => sub {
	my($id) = params->{'id'};
	
	unless (credential->{logged_in}) {
		redirect '/';
		return;
	}
	
	my $rule = getRule({ id => $id});
	
	unless ($rule) {
		redirect '/personal/?error=unknown';
		return;
	}

	unless ($rule->{user_id} == credential->{user}->{id}) {
		redirect '/personal/?error=unknown';
		return;
	}
	
	unless (deleteRule({ id => $rule->{id} })) {
		redirect '/personal/?error=unknown';
		return;
	}
	
	redirect '/personal';
};

1;